package com.glory.wxelectricitypda.activity;

import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.EqpLockInfoEntity;
import com.glory.wxelectricitypda.bean.MarkQueryBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class SealRetrievalActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.lseqpID)
    FreeUI_ClearEditText lseqpID;
    @BindView(R.id.lotID)
    FreeUI_ClearEditText lotID;
    @BindView(R.id.markNmae)
    TextView markNmae;
    @BindView(R.id.weekLy)
    TextView weekLy;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_seal_retrieval;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("印章调取");
        freeUI_titleView.setRightListener("调取印章", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (lseqpID.getText().toString().equals("")){
                    showToast("请扫描打标机",false);
                    return;
                }
                if (lotID.getText().toString().equals("")){
                    showToast("请扫描批次号",false);
                    return;
                }

                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("WeekLy", weekLy.getText().toString());
                    jsonObject.put("Lseqpid", lseqpID.getText().toString());
                    jsonObject.put("MarkNmae", markNmae.getText().toString());
                    jsonObject.put("LotID", lotID.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetUtils.requestPostUtils("sendEqpMarkNum","sendEqpMarkNum",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                        new TypeToken<ResponseBean<Object>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<ResponseBean<Object>> response) {
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showToast("调取成功",true);
                        }
                    }

                    @Override
                    public void onError(Response<ResponseBean<Object>> response) {
                        super.onError(response);
                        loadError(response.getException(), "selectEqpMarkNum");
                    }
                });

            }
        });

    }

    @Override
    protected void initData() {

        initEdit(lotID,s ->{

            if (lseqpID.getText().toString().equals("")) {
                showToast("请扫描打标机", false);
                return;
            }
            requestMark();
        });

        initEdit(lseqpID,s ->{

            if (lotID.getText().toString().equals("")) {
                showToast("请扫描批次ID", false);
                return;
            }
            requestMark();
        });


    }


    private void requestMark(){

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("LotID", lotID.getText().toString());
            jsonObject.put("Lseqpid", lseqpID.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("selectEqpMarkNum","selectEqpMarkNum",jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<MarkQueryBean>>(
                new TypeToken<ResponseBean<MarkQueryBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<MarkQueryBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("查询成功", true);
                    markNmae.setText(response.body().getBody().getMarkNmae());
                    weekLy.setText(response.body().getBody().getWeekLy());
                }
            }

            @Override
            public void onError(Response<ResponseBean<MarkQueryBean>> response) {
                super.onError(response);
                loadError(response.getException(), "selectEqpMarkNum");
            }
        });

    }



    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }
}